package com.greensecurity.sim.controller;

import com.greensecurity.sim.config.RoleNameConfig;
import com.greensecurity.sim.entity.GreenLand;
import com.greensecurity.sim.entity.GreenLandPhoto;
import com.greensecurity.sim.entity.Role;
import com.greensecurity.sim.entity.User;
import com.greensecurity.sim.service.GreenLandService;
import com.greensecurity.sim.service.UserService;
import com.greensecurity.sim.util.IsWindowOrLinuxUtils;
import com.greensecurity.sim.util.SplitUser;
import com.greensecurity.sim.util.batdownload.Base64Util;
import com.greensecurity.sim.util.batdownload.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/greenLand")
@Slf4j
@Api(description = "绿地上传信息控制层")
public class GreenLandController {
    @Resource
    private GreenLandService greenLandService;

    @Resource
    private UserService userService;

    @ResponseBody
    @ApiOperation(value = "通过绿地ID查询上传信息")
    @GetMapping("/findGreenLandById/{id}")
    public Optional<GreenLand> findGreenLandById(@PathVariable Long id) {
        return greenLandService.findGreenLandById(id);
    }
    @ResponseBody
    @ApiOperation(value = "查询全部绿地信息")
    @GetMapping("/findAll")
    public List<GreenLand> findAll() {
        List<GreenLand> list = greenLandService.findGreenLandAll();
        for(GreenLand greenLand : list){
            for(GreenLandPhoto greenLandPhoto : greenLand.getGreenLandPhotos()){
                greenLandPhoto.setPhotoUrl(IsWindowOrLinuxUtils.getPhotoUrl()+greenLandPhoto.getPhotoUrl());
            }
        }
        return list;
    }
    @ResponseBody
    @ApiOperation(value = "根据角色权限查询全部绿地信息 移动端")
    @PostMapping("/findAllByRole")
    public List<GreenLand> findAll(@RequestBody User user) {
        List<Role> role =user.getRole();
        List<GreenLand> list =new ArrayList<>();
        for(Role role1 :role){
            if (role1.getRoleName().equals(RoleNameConfig.INSERT)){
                list = user.getGreenLands();
            }else {
                list = greenLandService.findGreenLandAll();
                for(GreenLand greenLand : list){
                    for(GreenLandPhoto greenLandPhoto : greenLand.getGreenLandPhotos()){
                        greenLandPhoto.setPhotoUrl(IsWindowOrLinuxUtils.getPhotoUrl()+greenLandPhoto.getPhotoUrl());
                    }
                }
            }

        }
        return list;
    }
    @ResponseBody
    @ApiOperation(value = "随机查询5条绿地信息用于首页显示")
    @GetMapping("/findGreenLandLimit")
    public List<GreenLand> findGreenLandLimit() {
        List<GreenLand> list = greenLandService.findGreenLandLimit();
        for(GreenLand greenLand : list){
            for(GreenLandPhoto greenLandPhoto : greenLand.getGreenLandPhotos()){
                greenLandPhoto.setPhotoUrl(IsWindowOrLinuxUtils.getPhotoUrl()+greenLandPhoto.getPhotoUrl());
            }
        }
        return list;
    }
    @ResponseBody
    @ApiOperation(value = "保存绿地上传信息")
    @PostMapping("/save")
    public String save(@RequestBody User user) {
        SplitUser.splitUser(user);
        userService.save(user);
        return "保存成功";
    }

    @ResponseBody
    @ApiOperation(value = "删除绿地上传信息")
    @GetMapping("/del/{id}")
    public String del(@PathVariable Long id) {
        return greenLandService.delGreenLand(id);
    }

    @ApiOperation(value = "通过数据库用户头像的Url去生成Base64编码返回前端显示")
    @ResponseBody
    @RequiresPermissions("query")
    @PostMapping("/getBase64ThroughGreenLandPhotoUrl")
    public String getBase64ThroughGreenLandPhotoUrl(@RequestBody String PhotoUrl) {
//        JSONObject jsonObject = JSON.parseObject(String.valueOf(user))
        byte[] imgData;
        String imgStr;
        log.info(PhotoUrl);
        try {
            imgData = FileUtil.readFileByBytes(PhotoUrl);
            imgStr = Base64Util.encode(imgData);
        } catch (IOException e) {
            throw new RuntimeException("图片路径错误，请重新上传");
        }
        return "data:image/jpeg;base64,"+imgStr;
    }

    @ResponseBody
    @ApiOperation(value = "Web端 编辑绿地信息")
    @PostMapping("/updateWeb")
    public String updateWeb(@RequestBody GreenLand greenLand) {
        Optional<GreenLand> greenLand1 = greenLandService.findGreenLandById(greenLand.getId());
        GreenLand greenLand2 = greenLand1.get();
        greenLand.setGreenLandPhotos(greenLand2.getGreenLandPhotos());
        greenLandService.saveGreenLand(greenLand);
        return "保存成功";
    }

    @ResponseBody
    @ApiOperation(value = "编辑绿地信息")
    @PostMapping("/update")
    public String update(@RequestBody GreenLand greenLand) {
        Optional<GreenLand> greenLand1 = greenLandService.findGreenLandById(greenLand.getId());
        GreenLand greenLand2 = greenLand1.get();
        greenLand.setGreenLandPhotos(greenLand2.getGreenLandPhotos());
        greenLandService.saveGreenLand(greenLand);
        return "保存成功";
    }
}
